<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>if 判断的几种用法 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>if 判断的几种用法</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<h2 id="1-和文档相关的判断"><a href="#1-和文档相关的判断" class="headerlink" title="1.和文档相关的判断"></a>1.和文档相关的判断</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;shell 脚本中 if 还经常判断关于档案属性，比如判断是普通文件还是目录，判断文件是否有读写执行权限等。常用的也就几个选项：</p>
<ul>
<li>-e：判断文件或目录是否存在；</li>
<li>-d：判断是不是目录，并是否存在；<br>-f：判断是否是普通文件，并存在；</li>
<li>-r：判断文档是否有读权限；</li>
<li>-w：判断文档是否有写权限；</li>
<li>-x：判断是否可执行。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用 if 判断时，具体格式为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> [ -e filename ] ;<span class="keyword">then</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例子:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># if [ -d /home/ ];then echo ok;fi</span></div><div class="line">ok</div><div class="line">[root@192 sbin]<span class="comment"># if [ -f /home/ ];then echo ok;fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因为 /home/ 为目录非文件，所以 -f 并不会显示 “ok”。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># if [ -f /home/1.txt ];then echo ok;fi </span></div><div class="line">ok</div><div class="line">[root@192 sbin]<span class="comment"># if [ -r /home/1.txt ];then echo ok;fi</span></div><div class="line">ok</div><div class="line">[root@192 sbin]<span class="comment"># if [ -w /home/1.txt ];then echo ok;fi</span></div><div class="line">ok</div><div class="line">[root@192 sbin]<span class="comment"># if [ -x /home/1.txt ];then echo ok;fi</span></div><div class="line">[root@192 sbin]<span class="comment"># if [ -e /home/1.txt ];then echo ok;fi</span></div><div class="line">ok</div></pre></td></tr></table></figure>
<h2 id="2-变量是否为空"><a href="#2-变量是否为空" class="headerlink" title="2.变量是否为空"></a>2.变量是否为空</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有时候需要判断一个变量的值是否为空，以避免后续操作产生异常。如果不去判断变量是否有值，就接着在后续命令中引用该变量，则会出错。比如下面脚本：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim 3.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">n=`wc -l 1.txt|awk <span class="string">'&#123;print $1&#125;'</span>`</div><div class="line"><span class="keyword">if</span> [ <span class="variable">$n</span> -gt 10 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"The file 1.txt has more than 10 lines."</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个脚本看上去是没有问题，但没有考虑到 1.txt 文件不存在的情况，如果文件不存在，那么 n 的值也是不存在的。后面的判断也会出错。所以应该先判断一下 n 是否为空。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用下面方法即可：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim 4.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">a=</div><div class="line"><span class="keyword">if</span> [ -n <span class="string">"<span class="variable">$a</span>"</span> ]</div><div class="line"><span class="keyword">then</span></div><div class="line">   <span class="built_in">echo</span> <span class="string">"a is not null."</span></div><div class="line"><span class="keyword">else</span></div><div class="line">   <span class="built_in">echo</span> <span class="string">"a is null."</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/if%20%E5%88%A4%E6%96%AD%E7%9A%84%E5%87%A0%E7%A7%8D%E7%94%A8%E6%B3%95/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-n 选项可以判断一个变量是否不为空，注意一定要把变量引起来，上例用双引号把 $a 引起来了，否则是不对的，还有一个和 -n 正好相对的， -z ，用法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim 5.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">a=</div><div class="line"><span class="keyword">if</span> [ -z <span class="variable">$a</span> ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"a is null."</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/if%20%E5%88%A4%E6%96%AD%E7%9A%84%E5%87%A0%E7%A7%8D%E7%94%A8%E6%B3%95/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个 -z 后面的 $a 可以不用双引号引起来。</p>
<h2 id="3-if-判断条件可以是一条命令"><a href="#3-if-判断条件可以是一条命令" class="headerlink" title="3.if 判断条件可以是一条命令"></a>3.if 判断条件可以是一条命令</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># if grep -q '^yanyi:' /etc/passwd;then echo "user yanyi exist.";fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;grep -p 选项的作用是，过滤但不输出。用在 if 判断中，不需要输出结果，只需要知道它到底有没有执行成功，也就是说如果 /etc/passwd 文件中含有 yanyi 这个用户，那么条件就成立了。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/if%20%E5%88%A4%E6%96%AD%E7%9A%84%E5%87%A0%E7%A7%8D%E7%94%A8%E6%B3%95/03.png?raw=true" alt=""></figure></p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Shell/10. if 判断的几种用法/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
